package com.zzg.aop;

import com.zzg.entity.Log;
import com.zzg.entity.TmsUser;
import com.zzg.mapper.LogMapper;
import com.zzg.note.AdviceAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * @author zzg
 * @version 1.0.0
 * @ClassName：TheLogEntry
 * @Description: 描述
 * @date 2021/7/29
 **/
@Aspect//切面注解
@Component//构成
public class TheLogEntry {

    @Autowired
    private LogMapper logMapper;


    //设置在ManagerController的login方法的切点
    @Pointcut("execution(public * *TMS (..))")
    public void login(){//切点映射，命名不规定
        System.out.println("aspect");
    }

    @Around("@annotation(com.zzg.note.AdviceAnnotation) && @annotation(ad)")
    private Object studyAround(ProceedingJoinPoint pjp, AdviceAnnotation ad) throws Throwable{    // 环绕通知需要有返回值
        //执行方法
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpSession session= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        Map<String, String> map = System.getenv();
        String userName = map.get("USERNAME");// 电脑登录用户
        InetAddress addr = InetAddress.getLocalHost();
        String ip=addr.getHostAddress().toString(); //获取本机ip
        String hostName=addr.getHostName().toString(); //获取本机计算机名称
        String nethod = request.getMethod();
        int status = httpServletResponse.getStatus();
        String servletPath = request.getServletPath();
        Properties props=System.getProperties();
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        /*System.out.println(String.format("操作行为：", ad.name()));    // 获取切入方法上的注解
        System.out.println("TMS登录用户名："+tms_user.getTmsName());
        System.out.println("TMS登录账号："+tms_user.getAccount());
        System.out.println("电脑登录用户："+userName);
        System.out.println("本机名称："+hostName);
        System.out.println("本机IP："+ip);
        System.out.println("状态码："+status);
        System.out.println("时间："+format);
        System.out.println("请求方式："+nethod);
        System.out.println("请求路径："+servletPath);
        System.out.println("操作系统的名称："+props.getProperty("os.name"));
        System.out.println("操作系统的版本："+props.getProperty("os.version"));*/
        Log log=new Log();
        log.setLogoperation(ad.name());
        log.setLogtmsuser(tms_user.getTmsName());
        log.setLogaccount(tms_user.getAccount());
        log.setLogpcuser(userName);
        log.setLogpcname(hostName);
        log.setLogpcip(ip);
        log.setStatuscode(status+"");
        log.setLogdata(format);
        log.setRequestmethod(nethod);
        log.setRequestpath(servletPath);
        log.setLogos(props.getProperty("os.name"));
        log.setLogosversions(props.getProperty("os.version"));
        logMapper.insertLog(log);
        }
        return result;
    }




    /*//在本类的login执行之前
    @Before("login()")
    public void beforelogin(JoinPoint joinPoint){
        System.out.println("----------------------------前置通知---");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println("before");
    }*/
    //在本类的login执行之后执行
    /*@After("login()")
    public void afterlogin(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        String nethod = request.getMethod();//获取请求方式:
        String servletPath = request.getServletPath();//获取网名 也就是/login
        System.out.println("执行方法："+servletPath);
        System.out.println("请求方式："+nethod);
        TmsUser tms_user =(TmsUser)session.getAttribute("TMS_User");
        System.out.println(tms_user.getTmsName());
        System.out.println("after");
    }*/
}
